"""
Case Type   : HTAP行列融合特性
Case Name   : 开启enable_parallel_populate时，表中无数据进行转换
Create At   : 2025/1/6
Owner       : ningyali
Description :
    1.建表
    2.设置enable_parallel_populate为on
    3.进行全表转换
    4.进行部分列转换
    5.进行指定分区转换
    6.进行指定分区部分列转换
    7.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功
    6.执行成功
    7.执行成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.t_name = 't_htap_single_0127'

    def test_trans(self):
        text = '----step1:建表插入数据;expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name}_a;' \
              f'create table {self.t_name}_a(' \
              f'    c1 integer,' \
              f'    c2 varchar(30),' \
              f'    c3 char(30));'
        sql += f'drop table if exists {self.t_name}_b;' \
               f'create table {self.t_name}_b(' \
               f'    c1 integer(20),' \
               f'    c2 varchar(20) not null default \'\',' \
               f'    c3 char(30))' \
               f'partition by range (c1)(' \
               f'    partition {self.t_name}_b_p1 values less than (500),' \
               f'    partition {self.t_name}_b_p2 values less than (1000),' \
               f'    partition {self.t_name}_b_p3 values less than (3000),' \
               f'    partition {self.t_name}_b_p4 values less than (maxvalue));'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('CREATE TABLE'), 2, f'建表失败:{text}')

        text = '----step2:设置enable_parallel_populate为on; expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_parallel_populate=on')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        text = '----step3:进行全表转换; expect:执行成功----'
        self.log.info(text)
        sql = f'alter table {self.t_name}_a imcstored;' \
              f'alter table {self.t_name}_b imcstored;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), 2, text)

        text = '----step4:进行部分列转换; expect:执行成功----'
        self.log.info(text)
        sql = f'alter table {self.t_name}_a unimcstored;' \
              f'alter table {self.t_name}_b unimcstored;' \
              f'alter table {self.t_name}_a imcstored(c1, c3);' \
              f'alter table {self.t_name}_b imcstored(c1, c3);'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), 4, text)

        text = '----step5:进行指定分区转换; expect:执行成功----'
        self.log.info(text)
        sql = f'alter table {self.t_name}_b unimcstored;' \
              f'alter table {self.t_name}_b modify partition ' \
              f'{self.t_name}_b_p1 imcstored;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), 2, text)

        text = '----step6:进行指定分区部分列转换; expect:执行成功----'
        self.log.info(text)
        sql = f'alter table {self.t_name}_b modify partition ' \
              f'{self.t_name}_b_p1 unimcstored;' \
              f'alter table {self.t_name}_b modify partition ' \
              f'{self.t_name}_b_p1 imcstored(c1, c3);'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertEqual(sql_res.count('ALTER TABLE'), 2, text)

    def tearDown(self):
        text = '----step7:清理环境;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_parallel_populate')

        sql = f'drop table if exists {self.t_name}_a;' \
              f'drop table if exists {self.t_name}_b;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)

        self.assertEqual(True, guc_res, f'参数配置失败:{text}')
        self.assertEqual(sql_res.count('DROP TABLE'), 2, f'删除表失败:{text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
